package com.xhh.nphm.system.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xhh.nphm.system.domain.ReplyTemplate;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface ReplyTemplateDao extends BaseMapper<ReplyTemplate> {
    
    @Select("select PkId as replyNo,reply_content as replyRn from tm_reply_template where dept_id=#{deptId} and opt_user_id=#{userId} and opt_type=#{optType} and is_del = 0 order by sort asc")
    List<Map> selectRnByDeptOrUser(@Param("deptId") String deptId , @Param("userId") String userId, @Param("optType") Integer optType);

    @Select("select PkId as replyNo,reply_content as replyRn from tm_reply_template where opt_type=0 and is_del = 0")
    Map selectDefaultRn();

    @Select("select PkId,dept_name deptName,reply_content replyContent,opt_name optName,opt_time optTime,sort,opt_type optType " +
            "from tm_reply_template where dept_id = #{deptId} and is_del = 0 and opt_type != 0 " +
            "[[and reply_content =#{rTemp.replyContent}]] [[and opt_type=#{rTemp.optType}]] [[and opt_user=#{rTemp.optName}]]")
    List<Map<String, Object>> selectReplyListJsonByDept(@Param("deptId") String deptId, @Param("rTemp")ReplyTemplate replyTemplate);

    @Insert("insert into " +
            "tm_reply_template(dept_id,dept_name,reply_content,opt_user_id,opt_name,opt_time,sort,is_del,opt_type) " +
            "values (#{deptId},#{deptName},#{replyContent},#{optUserId},#{optName},#{optTime},#{sort},#{isDel},#{optType})")
    int saveReplyTemplate(ReplyTemplate replyTemplate);

    @Update("update tm_reply_template " +
            "set dept_id = #{deptId},dept_name=#{deptName},reply_content=#{replyContent}," +
            "    opt_user_id=#{optUserId},opt_name=#{optName},opt_time=#{optTime},opt_type=#{optType},sort=#{sort},is_del=#{isDel}  " +
            "where PkId = #{pkid,jdbcType=INTEGER}")
    int updateReplyTemplate(ReplyTemplate replyTemplate);

    @Select("select PkId pkid,dept_id deptId,dept_name deptName,reply_content replyContent,opt_user_id optUserId,opt_name optName,opt_time optTime,sort,opt_type optType,is_del isDel " +
            "from tm_reply_template where PkId=#{pkId,jdbcType=INTEGER} and is_del = 0")
    ReplyTemplate selectByPkId(Integer pkId);

    @Update("update tm_reply_template set is_del = 1 where PkId = #{pkId}")
    int removeReplyTemplate(@Param("pkId") Integer pkId);

    @Select("select PkId,dept_name deptName,reply_content replyContent,opt_name optName,opt_time optTime,sort,opt_type optType " +
            "from tm_reply_template where is_del = 0 " +
            "[[and reply_content =#{rTemp.replyContent}]] [[and opt_type=#{rTemp.optType}]] [[and opt_user=#{rTemp.optName}]] [[and dept_name=#{rTemp.deptName}]]")
    List<Map<String, Object>> selectReplyListJson(@Param("rTemp") ReplyTemplate replyTemplate);
}
